﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Mysoft.QuickCode.DAL.Util
{
    /// <summary>
    /// BaseEntityPlus工具类
    /// </summary>
    public static class BaseEntityPlusUtil
    {
        static Type baseEntityPlusType = null;
        static bool baseEntityPlusLoaded = false;
        static MethodInfo methodSyncDB = null;
        static MethodInfo methodDelete = null;

        static BaseEntityPlusUtil()
        {
            if (baseEntityPlusLoaded == false)
            {
                Assembly asm = AppDomain.CurrentDomain.Load("Mysoft.CSharpTools");
                if (asm != null)
                {
                    baseEntityPlusType = asm.GetType("Mysoft.CSharpTools.DAL.BaseEntityPlus");
                    methodSyncDB = baseEntityPlusType.GetMethod("SyncDB");
                    methodDelete = baseEntityPlusType.GetMethod("Delete");
                }

                baseEntityPlusLoaded = true;
            }
        }

        /// <summary>
        /// 是否属于BaseEntityPlus实例或子类
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool IsInstance(object entity)
        {
            if (entity.GetType().BaseType == baseEntityPlusType)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 标记为删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static void Delete(object entity)
        {
            methodDelete.Invoke(entity, null);
        }

        /// <summary>
        /// 同步到数据库
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool SyncDB(object entity)
        {
            return (bool)methodSyncDB.Invoke(entity, null);
        }
    }
}
